package com.woniuxy.book.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.book.controller.form.account.LoginForm;
import com.woniuxy.book.controller.form.account.UpdateAdminForm;
import com.woniuxy.book.controller.form.account.addAccountForm;
import com.woniuxy.book.controller.form.account.selectAdminForm;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.param.LoginParam;
import com.woniuxy.book.param.account.UpdateAdminParam;
import com.woniuxy.book.param.account.AddAccountParam;
import com.woniuxy.book.param.account.SelectAdminParam;
import com.woniuxy.book.service.AccountService;
import com.woniuxy.book.service.dto.AccountDTO;
import com.woniuxy.book.util.JWTUtil;
import com.woniuxy.book.util.RedisUtil;
import com.woniuxy.book.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@RestController
@RequestMapping("/account")
@Api(tags = "管理员相关接口")
public class AccountController {

    @Autowired
    private AccountService accountService;
    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/login")
    @ApiOperation("登录操作")
    public Result login(@RequestBody LoginForm form, HttpServletResponse resp){
        LoginParam loginParam = BeanUtil.toBean(form,LoginParam.class);
        AccountDTO accountDTO = accountService.login(loginParam);
        accountDTO.setRole("管理员");
        HashMap<String, Object> map = new HashMap<>();
        map.put("subject",accountDTO.getAccountId());
        map.put("role","管理员");
        String accessToken = JWTUtil.createToken(map, 30);//短token 有效时间30分钟
        String refreshToken = JWTUtil.createToken(map, 3 * 24 * 60);//长token 有效时间三天
        //通过响应头将jwt响应给客户端
        resp.setHeader("token", accessToken);
        redisUtil.set(accessToken,refreshToken);//存入redis中 短token为键 长token为值
        //暴露token
        resp.setHeader("Access-Control-Request-Headers","token");
        return Result.success("登录成功",accountDTO);
    }

    @PostMapping("/admin/addAccount")
    @ApiOperation("添加管理员")
    public Result addAccount(@RequestBody addAccountForm aaf){
        AddAccountParam param = BeanUtil.toBean(aaf,AddAccountParam.class);
        accountService.addAccount(param);
        return Result.success("管理员新增成功",null);
    }

    @GetMapping("/admin/selectAdmin")
    @ApiOperation("按条件查找管理员")
    public Result selectAdmin(selectAdminForm form){
        SelectAdminParam param = BeanUtil.toBean(form,SelectAdminParam.class);
        List<AccountDTO> accountDTOList = accountService.selectAdmin(param);
        return Result.success("查询管理员成功",accountDTOList);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value = "分页页码"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量"),
            @ApiImplicitParam(name = "condition",value = "昵称/电话")
    })
    @GetMapping("/admin/searchAdmin")
    @ApiOperation("按条件分页查找管理员")
    public Result searchAdmin(Integer pageNum,Integer pageSize,String condition){
        System.out.println(condition);
        PageInfo pageInfo = accountService.searchAdmin(pageNum,pageSize,condition);
        return Result.success("查询管理员成功",pageInfo);
    }

    @PostMapping("/admin/updateAdmin")
    @ApiOperation("修改管理员信息")
    public Result updateAdmin(@RequestBody UpdateAdminForm form){
        accountService.updateAdmin(BeanUtil.toBean(form, UpdateAdminParam.class));
        return Result.success("修改管理员成功",null);
    }

    @GetMapping("/admin/enableAccount")
    @ApiOperation("启用账号")
    public Result enableAccount(Integer accountId){
        accountService.enableAccount(accountId);
        return Result.success("启用该账号成功",null);
    }

    @GetMapping("/admin/disableAccount")
    @ApiOperation("禁用账号")
    public Result disableAccount(Integer accountId){
        accountService.disableAccount(accountId);
        return Result.success("禁用该账号成功",null);
    }

    @GetMapping("/admin/deleteAccount")
    @ApiOperation("删除账号")
    public Result deleteAccount(Integer accountId){
        accountService.deleteAccount(accountId);
        return Result.success("删除账号成功",null);
    }

    @GetMapping("/admin/undeleteAccount")
    @ApiOperation("删除账号")
    public Result undeleteAccount(Integer accountId){
        accountService.undeleteAccount(accountId);
        return Result.success("撤回删除账号成功",null);
    }

}

